package com.ckh.springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ckh.springboot.common.Result;
import com.ckh.springboot.entity.HomeWorkStudent;
import com.ckh.springboot.entity.HomeWorkTeacher;
import com.ckh.springboot.entity.User;
import com.ckh.springboot.mapper.HomeWorkTeacherMapper;
import com.ckh.springboot.mapper.UserMapper;
import com.ckh.springboot.service.IHomeWorkStudentService;
import com.ckh.springboot.service.IUserService;
import org.apache.ibatis.annotations.Param;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/homeWorkStudent")
@CrossOrigin(origins = "http://localhost:9528")
public class HomeWorkStudentController {
    @Resource
    IHomeWorkStudentService iHomeWorkStudentService ;
    @Resource
    HomeWorkTeacherMapper homeWorkTeacherMapper ;
    @Resource
    private IUserService userService;
    @Resource
    private UserMapper userMapper;
    @Resource
    private  IUserService iUserService ;
    /**
     * web 端 根据老师ID 获取 列表
     * @param id
     * @return
     */
    @GetMapping("/list")
    public Result list(@RequestParam("id") Integer id) {
        // 先查user表，获取student_id
        User user = userMapper.selectById(id);
        if (user == null) {
            return Result.error();
        }
        String studentId = user.getStudentId(); // 假设studentId是String类型，若为Integer请自行调整
        List<HomeWorkStudent> homeWorkStudentList = iHomeWorkStudentService.getHomeWorkStudentListByStudentId(studentId);
        return Result.success(homeWorkStudentList);
    }
    /**
     * web 端 根据homework_id 删除学生作业情况
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public Result delete(@Param("id") Integer id , @Param("studentId" ) Integer studentId) {
        int i = iHomeWorkStudentService.deleteHomeWorkStudentList(id, studentId);
        System.out.println(i);
        if(i > 0 ) return  Result.success(i);
        return Result.error();
    }
    /**
     * web 端 根据homework_id update学生作业情况
     * @param id
     * @return
     */
    @GetMapping("/update")
    public Result update(@Param("id") Integer id  , @Param("score") String score , @Param("studentId" ) Integer studentId) {
        int i = iHomeWorkStudentService.updateHomeWorkStudentList(id , score , studentId);
        System.out.println(i);
        if(i > 0 ) return  Result.success(i);
        return Result.error();
    }
    /**
     * 学生根据 homeWorkId 上交作业内容 小程序端
     */
    @GetMapping("/upload")
    public Result uploadHomeWork(@Param("id") Integer id
            , @Param("text") String text ,
                                 @Param("image") String image
            , @Param("name") String name
    ,@Param("studentId") Integer studentId
    ) {
        System.out.println(id);
        System.out.println(text);
        System.out.println(image);
        System.out.println(name);
        System.out.println(studentId);
        int i = iHomeWorkStudentService.uploadHomeWork(id, text , image , name , studentId);
        System.out.println(i);
        /**
         * 上交完作业 统计一下 studentid -  id - classid - 多个userId  那些人submitcount 总和
         */
//
//        QueryWrapper<HomeWorkStudent> queryWrapper= new QueryWrapper<>() ;
//        queryWrapper.eq("student_id" , id) ;
//        iUserService.getOne(queryWrapper);
//        homeWorkTeacherMapper.addSubmitCount(queryWrapper);
        if(i > 0 ) return  Result.success(i);
        return Result.error();
    }
    /**
     * 根据 作业Id 获取具体哪些人完成了 作业
     */
    @GetMapping("/getDoneHomeWorkStudent")
    public Result getDoneHomeWorkStudent(@RequestParam Integer homeId) {
        List<HomeWorkStudent> doneHomeWorkStudent = iHomeWorkStudentService.getDoneHomeWorkStudent(homeId);
        return Result.success(doneHomeWorkStudent);
    }
    /**
     *  根据学生id 和 作业id  获取 当前学生的作业  详情
     */
    @GetMapping("/detail")
    public Result getStudentHomeWorkDetail(@RequestParam Integer id,  @RequestParam Integer homeId) {
        List<HomeWorkStudent> studentHomeWorkDetail = iHomeWorkStudentService.getStudentHomeWorkDetail(id, homeId);
        return Result.success(studentHomeWorkDetail);
    }
    /**
     *  根据 学生 Id 和 作业id  给学生打分
     * @Param id  & homeId
     */
        @GetMapping("/addScore")
    public Result addScore (@RequestParam Integer id,  @RequestParam Integer homeId , @RequestParam String score) {
       int i = iHomeWorkStudentService.addScore(id, homeId, score ) ;
       if (i > 0 ) return  Result.success(i) ;
       return Result.error();
    }

}
